package com.ys.tdm.controller;

import java.util.Enumeration;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ys.tdm.bean.user.User;
import com.ys.tdm.service.user.IUserService;
import com.ys.tdm.util.JsonUtils;
import com.ys.tdm.util.SimpleReturnVo;

@Controller
@RequestMapping("/main")
public class MainController extends BaseController{
	private static Logger logger = Logger.getLogger(MainController.class);

	@Resource
	private IUserService userService;


	/**
	 * 登录验证
	 * @author Troy  
	 * @date 2017年12月27日 上午9:51:12 
	 * @param model
	 * @param user
	 * @param httpSession
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/login_check")
	public String loginCheck(ModelMap model,User user,HttpServletRequest request, HttpServletResponse response, String autoLogin,HttpSession httpSession) {
		SimpleReturnVo returnVo = new SimpleReturnVo(ERROR, "操作失败！");
		try {
			List<User> allMember = userService.getAll(user);
			User userInfor = new User();
			if (!allMember.isEmpty()) {
				userInfor = allMember.get(0);
				if (StringUtils.isNotBlank(userInfor.getId())) {
					//自动登录,设置cookies
					if (StringUtils.isNotBlank(autoLogin)&&"1".equals(autoLogin)) {
						Cookie cookie = new Cookie(USER_ID,userInfor.getId());
						cookie.setMaxAge(60*60*24*7);// 设置为一周有效  
			            cookie.setPath("/");  
			            cookie.setDomain(request.getServerName());
			            response.addCookie(cookie);
					}
					 
					httpSession.setAttribute("userInfor", userInfor); 
					returnVo.setResponseCode(SUCCESS);
					returnVo.setResponseMsg("操作成功");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
            logger.error("MainController-loginCheck",e);
		}
		return JsonUtils.toJson(returnVo);
	}
	
	/**
	 * 退出登录
	 * @author Troy  
	 * @date 2017年12月27日 上午9:50:58 
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/login_out")
	public String loginOut(ModelMap model, HttpServletRequest request) {
		HttpSession session = request.getSession();
		session.invalidate();
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
				+ path;
		Cookie[] cookies = request.getCookies();// 这样便可以获取一个cookie数组
		if (cookies.length > 0) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("userId")) {
					cookie.setMaxAge(0);
				}
			}
		}
		 return "redirect:"+basePath;
	}
}
